# encoding: utf-8
class Comment
  include Mongoid::Document

  field :content, type: String
  field :enable, type: Boolean
  field :layer, type: Integer
  field :name, type: String

  field :created_at, type: DateTime
  field :updated_at, type: DateTime

  embedded_in :article

  class << self
    def post(article, content, current_user)
      comment = nil
      Comment.transaction do
        comment= Comment.create! article_id: article.id, content: content, layer: article.all_comments.count+1,
                                 name: (current_user.name || current_user.username if current_user)

        article.comment_count+=1
        article.save!
      end
      comment
    end
  end
end